<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        height: 3000px;
      }
      div {
        position: absolute;
        left: 200px;
        top: 1500px;
        width: 200px;
        height: 200px;
        background-color: red;
        opacity: 0.3;
      }
    </style>
  </head>
  <body>
    <div id="demo" style="opacity: 0.3"></div>

    <script>
      function check(_id) {
        const demoEl = document.querySelector(`#${_id}`);
        if (demoEl.offsetTop <= window.pageYOffset + window.innerHeight) {
          demoEl.timer = setInterval(function () {
            if (demoEl.style.opacity === "1") {
              clearInterval(demoEl.timer);
              return (demoEl.timer = "");
            }
            demoEl.style.opacity = parseFloat(demoEl.style.opacity) + 0.01;
          }, 30);
        }
      }

      window.onscroll = function () {
        check("demo");
      };
    </script>
  </body>
</html>
